//
//  HWLoginView.m
//  嗨文
//
//  Created by 响当当 on 2019/3/19.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWLoginView.h"
#import "HWRegisterViewController.h"
#import "HWCommonWebViewController.h"

@interface HWLoginView()<UITextFieldDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView *backGroundView;//背景
@property (nonatomic, strong)UITextField *phoneNumTextField;//手机号码输入
@property (nonatomic, strong)UITextField *codeTextField;//密码输入
@property (nonatomic, strong)UIButton *loginBtn;//登陆
@property (nonatomic, strong)UIButton *registerBtn;//注册

@property (nonatomic, strong)UIButton *customerCodingBtn;//用户协议
@property (nonatomic, strong)UIButton *customerPrivideBtn;//隐私协议
@property (nonatomic, strong)UIButton *agreeImageBtn;//同意的图片

@property (nonatomic, strong)NSString *agreeCustomer;//同意用户协议

@end

@implementation HWLoginView

- (UIView *)backGroundView
{
    if (!_backGroundView)
    {
        _backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,250*kUIScaleBasePlus)];
        _backGroundView.backgroundColor = JSAMainColor;
    }
    return _backGroundView;
}

- (UITextField *)phoneNumTextField
{
    if (!_phoneNumTextField)
    {
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0,0,60*kUIScaleBasePlus,50*kUIScaleBasePlus)];
        leftView.backgroundColor= [UIColor clearColor];
        
        UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(19*kUIScaleBasePlus,16*kUIScaleBasePlus,12*kUIScaleBasePlus,18*kUIScaleBasePlus)];
        leftImageView.image = [UIImage imageNamed:@"telephone"];
        [leftView addSubview:leftImageView];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(49*kUIScaleBasePlus,17*kUIScaleBasePlus,1*kUIScaleBasePlus,16*kUIScaleBasePlus)];
        lineView.backgroundColor = JSALineColor;
        [leftView addSubview:lineView];
        
        
        _phoneNumTextField = [[UITextField alloc]initWithFrame:CGRectMake(40*kUIScaleBasePlus,CGRectGetMaxY(self.backGroundView.frame)+20*kUIScaleBasePlus,SCREEN_WIDTH-80*kUIScaleBasePlus,50*kUIScaleBasePlus)];
        _phoneNumTextField.delegate = self;
        _phoneNumTextField.layer.masksToBounds = YES;
        _phoneNumTextField.layer.cornerRadius = 8;
        _phoneNumTextField.layer.borderColor = JSALineColor.CGColor;
        _phoneNumTextField.layer.borderWidth = 0.2;
        _phoneNumTextField.leftView = leftView;
        _phoneNumTextField.leftViewMode = UITextFieldViewModeAlways;
        _phoneNumTextField.placeholder = @"请输入手机号码";
    }
    return _phoneNumTextField;
}

- (UITextField *)codeTextField
{
    if (!_codeTextField)
    {
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0,0,60*kUIScaleBasePlus,50*kUIScaleBasePlus)];
        leftView.backgroundColor= [UIColor clearColor];
        
        UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(19*kUIScaleBasePlus,16*kUIScaleBasePlus,12*kUIScaleBasePlus,18*kUIScaleBasePlus)];
        leftImageView.image = [UIImage imageNamed:@"password_edit"];
        [leftView addSubview:leftImageView];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(49*kUIScaleBasePlus,17*kUIScaleBasePlus,1*kUIScaleBasePlus,16*kUIScaleBasePlus)];
        lineView.backgroundColor = JSALineColor;
        [leftView addSubview:lineView];
        
        
        _codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(40*kUIScaleBasePlus,CGRectGetMaxY(self.phoneNumTextField.frame)+15*kUIScaleBasePlus,SCREEN_WIDTH-80*kUIScaleBasePlus,50*kUIScaleBasePlus)];
        _codeTextField.delegate = self;
        _codeTextField.layer.masksToBounds = YES;
        _codeTextField.layer.cornerRadius = 8;
        _codeTextField.layer.borderColor = JSALineColor.CGColor;
        _codeTextField.layer.borderWidth = 0.2;
        _codeTextField.leftView = leftView;
        _codeTextField.secureTextEntry = YES;
        _codeTextField.leftViewMode = UITextFieldViewModeAlways;
        _codeTextField.placeholder = @"请输入密码";
        [_codeTextField addTarget:self action:@selector(getUpKeyBoard) forControlEvents:UIControlEventAllTouchEvents];
    }
    return _codeTextField;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn)
    {
        _loginBtn = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-80*kUIScaleBasePlus,CGRectGetMaxY(self.codeTextField.frame)+25*kUIScaleBasePlus,160*kUIScaleBasePlus,48*kUIScaleBasePlus)
                                        textAligment:NSTextAlignmentCenter
                                                text:@"登陆"
                                            textClor:[UIColor whiteColor]
                                           buttonBGC:JSAMainColor
                                            textFont:20*kUIScaleBasePlus
                                              target:self
                                              method:@selector(login)];
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.cornerRadius = _loginBtn.frame.size.height/2;
    }
    return _loginBtn;
}

- (UIButton *)registerBtn
{
    if (!_registerBtn)
    {
        _registerBtn = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-60*kUIScaleBasePlus,CGRectGetMaxY(self.loginBtn.frame)+15*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                        textAligment:NSTextAlignmentCenter
                                                text:@"立即注册"
                                            textClor:JSAMainColor
                                           buttonBGC:[UIColor clearColor]
                                            textFont:12*kUIScaleBasePlus
                                              target:self
                                              method:@selector(registerPhone)];
    }
    return _registerBtn;
}


- (UIButton *)agreeImageBtn
{
    if (!_agreeImageBtn)
    {
        _agreeImageBtn = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-120*kUIScaleBasePlus-18*kUIScaleBasePlus,CGRectGetMaxY(self.registerBtn.frame)+15*kUIScaleBasePlus,18*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentCenter
                                                     text:@""
                                                 textClor:JSAMainColor
                                                buttonBGC:[UIColor clearColor]
                                                 textFont:12*kUIScaleBasePlus
                                                   target:self
                                                   method:@selector(changeSelect:)];
        [_agreeImageBtn setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
        _agreeImageBtn.selected = YES;
    }
    return _agreeImageBtn;
}

- (UIButton *)customerCodingBtn
{
    if (!_customerCodingBtn)
    {
        
        UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.agreeImageBtn.frame)+8*kUIScaleBasePlus,      CGRectGetMaxY(self.registerBtn.frame)+15*kUIScaleBasePlus,80*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentLeft
                                                     textClor:LCHexColor(0x333333)
                                                     textFont:10*kUIScaleBasePlus];
        titleLabel.text = @"已同意并阅读了";
        [self addSubview:titleLabel];
        
        _customerCodingBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)-10*kUIScaleBasePlus,CGRectGetMaxY(self.registerBtn.frame)+15*kUIScaleBasePlus,80*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentLeft
                                                         text:@"《用户协议》"
                                                     textClor:JSAMainColor
                                                    buttonBGC:[UIColor clearColor]
                                                     textFont:10*kUIScaleBasePlus
                                                       target:self
                                                       method:@selector(customerCoding)];
    }
    return _customerCodingBtn;
}

- (UIButton *)customerPrivideBtn
{
    if (!_customerPrivideBtn)
    {
        _customerPrivideBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.customerCodingBtn.frame)-10*kUIScaleBasePlus,CGRectGetMaxY(self.registerBtn.frame)+15*kUIScaleBasePlus,90*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentLeft
                                                          text:@"《隐私保护协议》"
                                                      textClor:JSAMainColor
                                                     buttonBGC:[UIColor clearColor]
                                                      textFont:10*kUIScaleBasePlus
                                                        target:self
                                                        method:@selector(customerPrivide)];
    }
    return _customerPrivideBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.backGroundView];
        [self addSubview:self.phoneNumTextField];
        [self addSubview:self.codeTextField];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registerBtn];
        [self addSubview:self.agreeImageBtn];
        [self addSubview:self.customerCodingBtn];
        [self addSubview:self.customerPrivideBtn];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideIndex:)];
        tap.numberOfTapsRequired =1;
        tap.numberOfTouchesRequired  =1;
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
        self.agreeCustomer = @"1";

    }
    return self;
}

#pragma mark - method
- (void)login
{
    
    
    if ([self.agreeCustomer isEqualToString:@"0"])
    {
        [SVProgressHUD showErrorWithStatus:@"请先同意用户协议和隐私政策"];
        DISMISS_SVP(1.0f);
        return;
    }
    
    if (![HWUtilties checkTel:self.phoneNumTextField.text]) return;

    if (!self.codeTextField.text)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        DISMISS_SVP(1.0f);
        return;
    }
    
    [self endEditing:YES];

    NSDictionary *pramaters = @{
                                @"phoneNum" : self.phoneNumTextField.text,
                                @"password" : self.codeTextField.text,

                                };
    [SVProgressHUD showWithStatus:nil];
    [RegisterAndLoginManager getLoginRequestWithParamers:pramaters sucess:^{
        DISMISS_SVP(1.0f);
        LCUserDefaultsSet(current_User.username,kLoginUser);
        UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (topRootViewController.presentedViewController)
        {
            topRootViewController = topRootViewController.presentedViewController;
        }
        
        [topRootViewController dismissViewControllerAnimated:YES completion:^{
            
        }];
    } fail:^(NSError *error) {
        DISMISS_SVP(1.0f);
    }];
}

- (void)registerPhone
{
    
    HWRegisterViewController *vc = [[HWRegisterViewController alloc]init];
    
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    vc.hidesBottomBarWhenPushed = YES;
    [topRootViewController presentViewController:vc animated:YES completion:nil];
    
}

#pragma mark
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.5 animations:^{
        [self setY:NAVIBAR_HEIGHT];
    }];
    
    return YES;
}

- (void)hideIndex:(UIGestureRecognizer *)sender
{
    [self endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        [self setY:NAVIBAR_HEIGHT];
    }];
}

- (void)getUpKeyBoard
{
    [UIView animateWithDuration:0.5 animations:^{
        [self setY:0];
    }];
}

- (void)changeSelect:(UIButton *)sender
{
    if (!sender.selected)
    {
        [sender setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
        sender.selected = YES;
        self.agreeCustomer = @"1";
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"unchoose"] forState:UIControlStateNormal];
        sender.selected = NO;
        self.agreeCustomer = @"0";
    }
}

- (void)customerCoding
{
    HWCommonWebViewController *tag = [[HWCommonWebViewController alloc]init];
    tag.urlStr = [NSString stringWithFormat:@"%@%@",WEBCOMME_HOST,@"/userAgreement"];
    tag.type = @"1";
    tag.titleStr = @"用户协议";
    tag.toUrl = nil;
    tag.toId = nil;
    tag.hidesBottomBarWhenPushed = YES;
    
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:tag animated:YES completion:nil];
}

- (void)customerPrivide
{
    HWCommonWebViewController *tag = [[HWCommonWebViewController alloc]init];
    tag.urlStr = [NSString stringWithFormat:@"%@%@",WEBCOMME_HOST,@"/privacyPolicy"];
    tag.type = @"1";
    tag.titleStr = @"用户隐私政策";
    tag.toUrl = nil;
    tag.toId = nil;
    tag.hidesBottomBarWhenPushed = YES;
    
    UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    
    [topRootViewController presentViewController:tag animated:YES completion:nil];
}

@end
